home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_se / assertion_collector.e < prev    next >
Text File  |  2000-03-25  |  7KB  |  246 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class ASSERTION_COLLECTOR
  17.    --
  18.    -- Singleton object in charge of assertion lookup.
  19.    -- This singleton is shared via the GLOBALS.`assertion_collector' once function.
  20.    --
  21.  
  22. inherit GLOBALS;
  23.  
  24. creation make
  25.  
  26. feature {NONE}
  27.  
  28.    processing_require: BOOLEAN;
  29.          -- When processing require collection only.
  30.  
  31.    header_comment: COMMENT;
  32.  
  33.    make is
  34.       do
  35.       end;
  36.  
  37. feature {RUN_CLASS}
  38.  
  39.    invariant_start is
  40.          -- Called to start the collection of a class invariant.
  41.       do
  42.          collector.clear;
  43.          header_comment := Void;
  44.       end;
  45.  
  46.    invariant_end(ct: TYPE): CLASS_INVARIANT is
  47.          -- Called to finish the collection of `ct' class invariant.
  48.       require
  49.          ct /= Void
  50.       local
  51.          r: ARRAY[ASSERTION];
  52.          position: POSITION;
  53.       do
  54.          r := runnable(collector,ct,Void,'_');
  55.          if r /= Void then
  56.             small_eiffel.incr_magic_count;
  57.             position := ct.base_class.name.start_position;
  58.             !!Result.make_runnable(position,r,ct,Void);
  59.             Result.set_header_comment(header_comment);
  60.          end;
  61.       end;
  62.  
  63. feature {BASE_CLASS}
  64.  
  65.    invariant_add_last(ci: CLASS_INVARIANT) is
  66.       require
  67.          ci /= Void
  68.       local
  69.          hc2: COMMENT;
  70.          bc, bc2: BASE_CLASS;
  71.       do
  72.          ci.add_into(collector);
  73.          hc2 := ci.header_comment;
  74.          if header_comment = Void then
  75.             header_comment := hc2;
  76.          elseif hc2 = Void then
  77.          else
  78.             bc := header_comment.start_position.base_class;
  79.             bc2 := hc2.start_position.base_class;
  80.             if bc2 = bc or else bc2.is_subclass_of(bc) then
  81.                header_comment := hc2;
  82.             end;
  83.          end;
  84.       end;
  85.  
  86.    require_start is
  87.          -- Called to start the collection of require assertions.
  88.       do
  89.          collector.clear;
  90.          header_comment := Void;
  91.          processing_require := true;
  92.       end;
  93.  
  94.    require_end(rf: RUN_FEATURE; ct: TYPE): RUN_REQUIRE is
  95.          -- Called to finish the collection of `ct' require assertion.
  96.       require
  97.          ct = rf.current_type
  98.       local
  99.          i: INTEGER;
  100.          sp: POSITION;
  101.          bc: BASE_CLASS;
  102.          a: ASSERTION;
  103.          r, r2: ARRAY[ASSERTION];
  104.          er: E_REQUIRE;
  105.       do
  106.          processing_require := false;
  107.          r := runnable(collector,ct,rf,'R');
  108.          if r /= Void then
  109.             from
  110.                !!r2.with_capacity(r.count,1);
  111.                a := r.item(1);
  112.                r2.add_last(a);
  113.                sp := a.start_position;
  114.                bc := sp.base_class;
  115.                i := 2;
  116.             until
  117.                i > r.upper or else r.item(i).start_position.base_class /= bc
  118.             loop
  119.                r2.add_last(r.item(i));
  120.                i := i + 1;
  121.             end;
  122.             !!er.make_runnable(sp,r2,ct,rf);
  123.             !!Result.make(er);
  124.             from
  125.             until
  126.                i > r.upper
  127.             loop
  128.                from
  129.                   !!r2.with_capacity(r.count,1);
  130.                   a := r.item(i);
  131.                   r2.add_last(a);
  132.                   sp := a.start_position;
  133.                   bc := sp.base_class;
  134.                   i := i + 1;
  135.                until
  136.                   i > r.upper or else r.item(i).start_position.base_class /= bc
  137.                loop
  138.                   r2.add_last(r.item(i));
  139.                   i := i + 1;
  140.                end;
  141.                !!er.make_runnable(sp,r2,ct,rf);
  142.                Result.add(er);
  143.             end;
  144.          end;
  145.       end;
  146.  
  147.    ensure_start is
  148.          -- Called to start the collection of ensure assertions.
  149.       do
  150.          collector.clear;
  151.          header_comment := Void;
  152.       end;
  153.  
  154.    ensure_end(rf: RUN_FEATURE; ct: TYPE): E_ENSURE is
  155.          -- Called to finish the collection of `ct' ensure assertion.
  156.       require
  157.          ct = rf.current_type
  158.       local
  159.          r: ARRAY[ASSERTION];
  160.          position: POSITION;
  161.       do
  162.          r := runnable(collector,ct,rf,'E');
  163.          if r /= Void then
  164.             position := ct.base_class.name.start_position;
  165.             !!Result.make_runnable(position,r,ct,rf);
  166.             Result.set_header_comment(header_comment);
  167.          end;
  168.       end;
  169.  
  170.    assertion_add_last(f: E_FEATURE) is
  171.          -- To add some require/ensure assertion.
  172.       local
  173.          r: E_REQUIRE;
  174.          e: E_ENSURE;
  175.       do
  176.          if processing_require then
  177.             r := f.require_assertion;
  178.             if r /= Void then
  179.                if header_comment = Void then
  180.                   header_comment := r.header_comment;
  181.                end;
  182.                r.add_into(collector);
  183.             end;
  184.          else
  185.             e := f.ensure_assertion;
  186.             if e /= Void then
  187.                if header_comment = Void then
  188.                   header_comment := e.header_comment;
  189.                end;
  190.                e.add_into(collector);
  191.             end;
  192.          end;
  193.       end;
  194.  
  195. feature {ASSERTION_LIST}
  196.  
  197.    runnable(collected: ARRAY[ASSERTION];
  198.             ct: TYPE;
  199.             for:RUN_FEATURE;
  200.             assertion_check_tag: CHARACTER): ARRAY[ASSERTION] is
  201.          -- Produce a runnable `collected'.
  202.       require
  203.          collected.lower = 1;
  204.          for /= Void implies ct = for.current_type;
  205.       local
  206.          i: INTEGER;
  207.          a: ASSERTION;
  208.       do
  209.          if not collected.is_empty then
  210.             from
  211.                Result := collected.twin;
  212.                i := Result.upper;
  213.             until
  214.                i = 0
  215.             loop
  216.                small_eiffel.push(for);
  217.                a := Result.item(i).to_runnable(ct,assertion_check_tag);
  218.                if a = Void then
  219.                   error(Result.item(i).start_position,fz_bad_assertion);
  220.                else
  221.                   Result.put(a,i);
  222.                end;
  223.                small_eiffel.pop;
  224.                i := i - 1;
  225.             end;
  226.          end;
  227.       end;
  228.  
  229. feature {NONE}
  230.  
  231.    collector: ARRAY[ASSERTION] is
  232.       once
  233.          !!Result.make(1,12);
  234.       end;
  235.  
  236.    singleton_memory: ASSERTION_COLLECTOR is
  237.       once
  238.          Result := Current;
  239.       end;
  240.  
  241. invariant
  242.  
  243.    is_real_singleton: Current = singleton_memory
  244.  
  245. end -- ASSERTION_COLLECTOR
  246.